package com.cw.lang.common.config;

import java.util.Properties;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cw.lang.common.type.EnumTypeHandler;

import tk.mybatis.mapper.autoconfigure.ConfigurationCustomizer;

/**
 * @author chenw
 * @version $$Id: MybatisConfiguration.java, v 0.1 2018/10/18 上午9:29 chenw Exp $$
 */
@Configuration
public class MybatisConfiguration {

    /**
     * 针对于实现了IEnum接口的枚举，dal层可直接做类型转换
     *
     * @return {@link ConfigurationCustomizer}
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.getTypeHandlerRegistry()
            .register(EnumTypeHandler.class);
    }

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties p = new Properties();
        p.setProperty("Oracle", "oracle");
        p.setProperty("MySQL", "mysql");
        p.setProperty("PostgreSQL", "postgresql");
        p.setProperty("DB2", "db2");
        p.setProperty("SQL Server", "sqlserver");
        databaseIdProvider.setProperties(p);
        return databaseIdProvider;
    }
}
